package me;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author yexy
 * @date 2020/6/30 4:58 PM
 * @description
 */
public class Server {

    public static void main(String[] args) {

        ServerSocket serverSocket = null;
        Socket socket = null;
        ObjectInputStream objectInputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            serverSocket = new ServerSocket(9999);
            socket = serverSocket.accept();

            objectInputStream = new ObjectInputStream(socket.getInputStream());

            UserMessage userMessage = (UserMessage) objectInputStream.readObject();
            System.out.println("用户登录的信息是："+userMessage.getUser().toString());

            if (userMessage.getUser().getUserName().equals("admin")
                    && userMessage.getUser().getPassword().equals("123456")){
                userMessage.setType("success");
            }else
                userMessage.setType("fail");

            objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(userMessage);

        } catch (Exception e) {
            e.printStackTrace();
        }finally
        {

            try {
                if (objectInputStream != null)
                    objectInputStream.close();
                if (objectOutputStream != null)
                    objectOutputStream.close();
                if (socket != null)
                    socket.close();
                if(serverSocket != null )
                    serverSocket.close();
            }catch (Exception e){
                e.printStackTrace();
            }


        }
    }


}
